<template>
  <div>
    <el-form ref="ruleForm" :model="formData" :inline="true" class="form" label-position="left" label-width="150px">
      <el-form-item label="文档地址:">
        <el-upload ref="uploads" class="upload-demo" drag accept=".xls,.xlsx" :limit="xlsNumber" :auto-upload="false"
          :action="uploadFile" :headers="headers" :file-list="fileList" :on-exceed="handleExceed" multiple>
          <i class="el-icon-upload" />
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div slot="tip" class="el-upload__tip">
            只能上传.xls, .xlsx后缀文件
          </div>
        </el-upload>
        <div style="display: flex; justify-content: center">
          <el-button size="mini" @click="resetForm('ruleForm')">取消</el-button>
          <el-button size="mini" type="primary" @click="submitForm('ruleForm')">上传</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    props: {
      xlsNumber: {
        type: Number,
        default: 1,
      },
      close: {
        type: Function,
        default: null,
      },
      fullDraw: {
        type: Function,
        default: null,
      },
    },
    data() {
      return {
        ruleForm: {},
        formData: {},
        fileList: []
      };
    },
    computed: {
      uploadFile() {
        return process.env.VUE_APP_BASE_API + "/dc/importCustomer";
      },
      headers() {
        const head = {
          token: this.$store.getters.token,
        };
        return head;
      },
    },
    methods: {
      // 判断文件类型方法
      // onChangeFile(file, fileList) {
      //   const whiteList = ["xlsx", "xls"];
      //   const fileSuffix = file.name.split(".").pop();
      //   if (whiteList.indexOf(fileSuffix) === -1) {
      //     this.$message({
      //       message: "请上传xlsx格式的文件",
      //       type: "error",
      //     });
      //     fileList.pop();
      //   }
      //   if (file.response && file.response.code != 200) {
      //     this.$message({
      //       showClose: true,
      //       dangerouslyUseHTMLString: true,
      //       message: file.response.message,
      //       type: "error",
      //       duration: "0",
      //       customClass: "elmessage",
      //     });
      //   }
      // },
      resetForm(formName) {
        // this.$emit("close");
        this.close()
      },
      submitForm(formName) {
        if (this.$refs.uploads.uploadFiles.length == 0) {
          this.$message({
            type: "info",
            message: "请选择至少一个文件",
          });
          return;
        } else {
          // 自定义导入
          this.$refs[formName].validate((valid) => {
            if (valid) {
              this.$refs.uploads.submit();
              // this.$emit("fullDraw");
              setTimeout(() => {
                this.fullDraw()
              }, 1000)
            } else {
              console.log("error submit!!");
              return false;
            }
          });
          // this.$emit("close");
          this.close()
          this.$refs.uploads.uploadFiles = [];
        }
      },
      handleExceed(files, fileList) {
        this.$message.warning(
          `当前限制选择 ${this.xlsNumber} 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
        );
      },
    },
  };
</script>

<style>
</style>
